using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Visuals;

namespace BENBUN.ExportCRS {
    public partial class OptionsView : UserControl {
        public OptionsView() {
            InitializeComponent();
            checkTurns.CheckedChanged += new EventHandler(checkTurns_CheckedChanged);
            checkValleys.CheckedChanged += new EventHandler(checkValleys_CheckedChanged);
            checkSummits.CheckedChanged += new EventHandler(checkSummits_CheckedChanged);
            checkMaxLength.CheckedChanged += new EventHandler(checkMaxLength_CheckedChanged);

            valleyAscUpDown.ValueChanged += new EventHandler(valleyAscUpDown_Changed);
            valleyDecUpDown.ValueChanged += new EventHandler(valleyDecUpDown_Changed);
            summitAscUpDown.ValueChanged += new EventHandler(summitAscUpDown_Changed);
            summitDecUpDown.ValueChanged += new EventHandler(summitDecUpDown_Changed);
            maxLengthUpDown.ValueChanged += new EventHandler(maxLengthUpDown_Changed);

            angleUpDown.ValueChanged += new EventHandler(turnAngle_Changed);
            comboNameSource.SelectedValueChanged += new EventHandler(nameSource_Changed);
            comboDistanceUnits.SelectedValueChanged += new EventHandler(comboDistanceUnits_Changed);
            


        }

        public IPageStatus Status {
            get { return null; }
        }

        public bool Modified {
            get { return false; }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void ShowPage(string bookmark) {
            bIgnoreCtrlChanges = true;
            bIgnoreCtrlChanges = false;

            angleUpDown.Value = (decimal)BenbunPlugin.Instance.turnAngle;
            summitAscUpDown.Value = (decimal)BenbunPlugin.Instance.summitSizeAsc;
            summitDecUpDown.Value = (decimal)BenbunPlugin.Instance.summitSizeDec;

            valleyAscUpDown.Value = (decimal)BenbunPlugin.Instance.valleySizeAsc;
            valleyDecUpDown.Value = (decimal)BenbunPlugin.Instance.valleySizeDec;
            maxLengthUpDown.Value = (decimal)BenbunPlugin.Instance.maxLength;

            bIgnoreCtrlChanges = true;
            checkTurns.Checked = !BenbunPlugin.Instance.doTurns;    
            checkSummits.Checked = !BenbunPlugin.Instance.doSummits;
            checkValleys.Checked = !BenbunPlugin.Instance.doValleys;
            checkMaxLength.Checked = !BenbunPlugin.Instance.doMaxLength;
            
            bIgnoreCtrlChanges = false;
            checkTurns.Checked = BenbunPlugin.Instance.doTurns;
            checkValleys.Checked = BenbunPlugin.Instance.doValleys;
            checkSummits.Checked = BenbunPlugin.Instance.doSummits;
            checkMaxLength.Checked = BenbunPlugin.Instance.doMaxLength;

            if (BenbunPlugin.Instance.nameSourceLocation == "location")
            {
                comboNameSource.SelectedIndex = 0;
            }
            else
            {
                comboNameSource.SelectedIndex = 1;
            }

            if (BenbunPlugin.Instance.distanceUnits== "meters")
            {
                comboDistanceUnits.SelectedIndex = 0;
                lblSummit1.Text = "meters";
                lblSummit2.Text = "meters";
                lblValley1.Text = "meters";
                lblValley3.Text = "meters";
                lblMaxLength.Text = "meters apart";
            }
            else
            {
                comboDistanceUnits.SelectedIndex = 1;
                lblSummit1.Text = "feet";
                lblSummit2.Text = "feet";
                lblValley1.Text = "feet";
                lblValley3.Text = "feet";
                lblMaxLength.Text = "feet apart";
            }          
        }

        public bool HidePage() {
            return true;
        }

        public void ThemeChanged(ITheme visualTheme) {
            BackColor = visualTheme.Control;
            //_navigateToBrowserAfterUploadCheckBox.ForeColor = visualTheme.ControlText;
        }

        public void UICultureChanged(CultureInfo culture) {
        }

        #region Event handlers

        void comboDistanceUnits_Changed(object sender, EventArgs e)
        {
            if (bIgnoreCtrlChanges) return;

            if (comboDistanceUnits.SelectedIndex == 0)
            {
                BenbunPlugin.Instance.distanceUnits = "meters";
                lblSummit1.Text = "meters";
                lblSummit2.Text = "meters";
                lblValley1.Text = "meters";
                lblValley3.Text = "meters";
                lblMaxLength.Text = "meters apart";
            }
            else
            {
                BenbunPlugin.Instance.distanceUnits = "feet";
                lblSummit1.Text = "feet";
                lblSummit2.Text = "feet";
                lblValley1.Text = "feet";
                lblValley3.Text = "feet";
                lblMaxLength.Text = "feet apart";
            }

        }

        void nameSource_Changed(object sender, EventArgs e)
        {
            if (bIgnoreCtrlChanges) return;

            if (comboNameSource.SelectedIndex == 0)
            {
                BenbunPlugin.Instance.nameSourceLocation = "location";
            }
            else
            {
                BenbunPlugin.Instance.nameSourceLocation = "name";
            }

        }
        void checkTurns_CheckedChanged(object sender, EventArgs e)
        {

            angleUpDown.Enabled = checkTurns.Checked;
            lblAngle.Enabled = checkTurns.Checked;

            if (bIgnoreCtrlChanges) return;
            BenbunPlugin.Instance.doTurns = checkTurns.Checked;

        }

        void checkValleys_CheckedChanged(object sender, EventArgs e)
        {
            lblValley1.Enabled = checkValleys.Checked;
            lblValley2.Enabled = checkValleys.Checked;
            lblValley3.Enabled = checkValleys.Checked;
            valleyAscUpDown.Enabled = checkValleys.Checked;
            valleyDecUpDown.Enabled = checkValleys.Checked;

            if (bIgnoreCtrlChanges) return;
            BenbunPlugin.Instance.doValleys = checkValleys.Checked;
       }

        void checkMaxLength_CheckedChanged(object sender, EventArgs e)
        {
            lblMaxLength.Enabled = checkMaxLength.Checked;
            maxLengthUpDown.Enabled = checkMaxLength.Checked;

            if (bIgnoreCtrlChanges) return;
            BenbunPlugin.Instance.doMaxLength = checkMaxLength.Checked;

        }

        void checkSummits_CheckedChanged(object sender, EventArgs e)
        {
            lblSummit1.Enabled = checkSummits.Checked;
            lblSummit2.Enabled = checkSummits.Checked;
            lblSummit3.Enabled = checkSummits.Checked;
            summitAscUpDown.Enabled = checkSummits.Checked;
            summitDecUpDown.Enabled = checkSummits.Checked;

            if (bIgnoreCtrlChanges) return;
            BenbunPlugin.Instance.doSummits = checkSummits.Checked;
            
        }

        void valleyAscUpDown_Changed(object sender, EventArgs e)
        {
            BenbunPlugin.Instance.valleySizeAsc = (double)valleyAscUpDown.Value;
        }

        void valleyDecUpDown_Changed(object sender, EventArgs e)
        {
            BenbunPlugin.Instance.valleySizeDec = (double)valleyDecUpDown.Value;
        }

        void summitAscUpDown_Changed(object sender, EventArgs e)
        {
            BenbunPlugin.Instance.summitSizeAsc = (double)summitAscUpDown.Value;
        }

        void summitDecUpDown_Changed(object sender, EventArgs e)
        {
            BenbunPlugin.Instance.summitSizeDec = (double)summitDecUpDown.Value;
        }

        void maxLengthUpDown_Changed(object sender, EventArgs e)
        {
            BenbunPlugin.Instance.maxLength = (double)maxLengthUpDown.Value;
        }

        void turnAngle_Changed(object sender, EventArgs e)
        {
            BenbunPlugin.Instance.turnAngle = (double)angleUpDown.Value;
            
        }


        #endregion

        private bool bIgnoreCtrlChanges = false;

     }
}